package com.chixigua.chaos.config.security.jwt;

import com.alibaba.fastjson.JSONObject;
import com.chixigua.chaos.utils.ErrorEnum;
import com.chixigua.chaos.utils.JsonResult;
import com.fasterxml.jackson.core.JsonToken;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
public class JwtAuthController {


    @Resource
    JwtAuthService jwtAuthService;

    @RequestMapping(value = "/authentication")
    public JSONObject login(@RequestBody Map<String,String> map){
        String username  = map.get("userName");
        String password = map.get("password");
        try {
            return JsonResult.successJson(jwtAuthService.login(username, password));
        }catch (Exception e){
            return JsonResult.errorJson(ErrorEnum.E_10023);
        }
    }

    @RequestMapping(value = "/refreshtoken")
    public  JSONObject refresh(@RequestHeader("${jwt.header}") String token){
            return JsonResult.successJson(jwtAuthService.refreshToken(token));
    }

    @RequestMapping("/hello")
//    @PreAuthorize("hasAuthority('WRITE')")
    @PreAuthorize("hasRole('ROLE_admin')")
    public String hello() {
        return "world";
    }

    @RequestMapping("/hello1")
    @PreAuthorize("hasAuthority('WRITE')")
//    @PreAuthorize("hasRole('ROLE_admin')")
    public String hello1() {
        return "world1";
    }


    @RequestMapping("/hello2")
    public String hello2() {
        return "world2";
    }

    @RequestMapping("/hello3")
    public String hello3() {
        return "world3";
    }

}
